﻿/*----------------------------------------------------------------------------------------
*
*  Copyright 2019, Gao Hai Hui, <fromzeropoint@126.com>.  All rights reserved.
*  https://gitee.com/helloworldghh/xoskit.git
*  Use of this source code is governed by a MIT license
*  that can be found in the License file.
*
----------------------------------------------------------------------------------------*/
#ifndef __XOSKIT_XOS_CORE_XOS_STL_SRC_PROTOCOL_BASE_BASE_H__
#define __XOSKIT_XOS_CORE_XOS_STL_SRC_PROTOCOL_BASE_BASE_H__

#include "../../import/head.h"

namespace xos_box
{
	class i_serial;
	class i_buf;
}

namespace xos_protocol
{

	class base
	{
	public:
        //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        // 
		base( xos_box::i_serial * pSerial, xos_box::i_buf * pBuf ) :
			m_pSerial( pSerial ),
			m_pBuf( pBuf ) 
		{
		}

		~base()
		{
		}

	protected:
        //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        // 
		xos_box::i_serial * m_pSerial;
		xos_box::i_buf * m_pBuf;

	public:
        //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
		// 
		// set function
		// 
		void set_serial( xos_box::i_serial * pSerial )
		{
			m_pSerial = pSerial;
		}

		void set_buf( xos_box::i_buf * pBuf )
		{
			m_pBuf = pBuf;
		}

        //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        // 
        int un_pack_str_array( int nBufSize, char * lppszStr, int nStrNum, int * lpnStrNum, int * lpnStrNumNeeded )
        {
            int ret = 0;
            ret = m_pSerial->un_pack_str_array( m_pBuf, nBufSize, lppszStr, nStrNum, lpnStrNum, lpnStrNumNeeded );
            return ret;
        }
		int un_pack_str( char * lpszStr, int nBufSize, int * lpnStrLen, int * lpnStrLenNeeded )
		{
			int ret = 0;
			ret = m_pSerial->un_pack_str( m_pBuf, lpszStr, nBufSize, lpnStrLen, lpnStrLenNeeded );
			return ret;
		}
        int pack_str_array( int * lpnAbsolutePos, int nBufSize, const char * lppszStr, int nStrNum )
        {
            int ret = 0;
            ret = m_pSerial->pack_str_array( m_pBuf, lpnAbsolutePos, nBufSize, lppszStr, nStrNum );
            return ret;
        }
		int pack_str( int * lpnAbsolutePos, const char * lpszStr )
		{
			int ret = 0;
			ret = m_pSerial->pack_str( m_pBuf, lpnAbsolutePos, lpszStr );
			return ret;
		}
        int pack_str_array( int nBufSize, const char * lppszStr, int nStrNum )
        {
            int ret = 0;
            ret = m_pSerial->pack_str_array( m_pBuf, nBufSize, lppszStr, nStrNum );
            return ret;
        }
		int pack_str( const char * lpszStr )
		{
			int ret = 0;
			ret = m_pSerial->pack_str( m_pBuf, lpszStr );
			return ret;
		}

        //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        // 
        int un_pack_mem_array( int nBufSize, char * lppszMem, int * lpnMemLen, int nMemNum, int * lpnMemNum, int * lpnMemNumNeeded )
        {
            int ret = 0;
            ret = m_pSerial->un_pack_mem_array( m_pBuf, nBufSize, lppszMem, lpnMemLen, nMemNum, lpnMemNum, lpnMemNumNeeded );
            return ret;
        }
		int un_pack_mem( char * lpszMem, int nBufSize, int * lpnMemLen, int * lpnMemLenNeeded )
		{
			int ret = 0;
			ret = m_pSerial->un_pack_mem( m_pBuf, lpszMem, nBufSize, lpnMemLen, lpnMemLenNeeded );
			return ret;
		}
        int pack_mem_array( int * lpnAbsolutePos, int nBufSize, const char * lppszMem, int * lpnMemLen, int nMemNum )
        {
            int ret = 0;
            ret = m_pSerial->pack_mem_array( m_pBuf, lpnAbsolutePos, nBufSize, lppszMem, lpnMemLen, nMemNum );
            return ret;
        }
		int pack_mem( int * lpnAbsolutePos, const char * lpszMem, int nMemLen )
		{
			int ret = 0;
			ret = m_pSerial->pack_mem( m_pBuf, lpnAbsolutePos, lpszMem, nMemLen );
			return ret;
		}
        int pack_mem_array( int nBufSize, const char * lppszMem, int * lpnMemLen, int nMemNum )
        {
            int ret = 0;
            ret = m_pSerial->pack_mem_array( m_pBuf, nBufSize, lppszMem, lpnMemLen, nMemNum );
            return ret;
        }
		int pack_mem( const char * lpszMem, int nMemLen )
		{
			int ret = 0;
			ret = m_pSerial->pack_mem( m_pBuf, lpszMem, nMemLen );
			return ret;
		}

        //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        // 
        int un_pack_int8_array( xos::xos_u8 * lpuData, int nNum, int * lpnNum, int * lpNumNeeded )
        {
            int ret = 0;
            ret = m_pSerial->un_pack_int8_array( m_pBuf, lpuData, nNum, lpnNum, lpNumNeeded );
            return ret;
        }
		int un_pack_int8( xos::xos_u8 * lpuData )
		{
			int ret = 0;
			ret = m_pSerial->un_pack_int8( m_pBuf, lpuData );
			return ret;
		}
        int pack_int8_array( int * lpnAbsolutePos, xos::xos_u8 * lpuData, int nNum )
        {
            int ret = 0;
            ret = m_pSerial->pack_int8_array( m_pBuf, lpnAbsolutePos, lpuData, nNum );
            return ret;
        }
		int pack_int8( int * lpnAbsolutePos, xos::xos_u8 uData )
		{
			int ret = 0;
			ret = m_pSerial->pack_int8( m_pBuf, lpnAbsolutePos, uData );
			return ret;
		}
        int pack_int8_array( xos::xos_u8 * lpuData, int nNum )
        {
            int ret = 0;
            ret = m_pSerial->pack_int8_array( m_pBuf, lpuData, nNum );
            return ret;
        }
		int pack_int8( xos::xos_u8 uData )
		{
			int ret = 0;
			ret = m_pSerial->pack_int8( m_pBuf, uData );
			return ret;
		}

        //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        // 
        int un_pack_int16_array( xos::xos_u16 * lpuData, int nNum, int * lpnNum, int * lpNumNeeded )
        {
            int ret = 0;
            ret = m_pSerial->un_pack_int16_array( m_pBuf, lpuData, nNum, lpnNum, lpNumNeeded );
            return ret;
        }
		int un_pack_int16( xos::xos_u16 * lpuData )
		{
			int ret = 0;
			ret = m_pSerial->un_pack_int16( m_pBuf, lpuData );
			return ret;
		}
        int pack_int16_array( int * lpnAbsolutePos, xos::xos_u16 * lpuData, int nNum )
        {
            int ret = 0;
            ret = m_pSerial->pack_int16_array( m_pBuf, lpnAbsolutePos, lpuData, nNum );
            return ret;
        }
		int pack_int16( int * lpnAbsolutePos, xos::xos_u16 uData )
		{
			int ret = 0;
			ret = m_pSerial->pack_int16( m_pBuf, lpnAbsolutePos, uData );
			return ret;
		}
        int pack_int16_array( xos::xos_u16 * lpuData, int nNum )
        {
            int ret = 0;
            ret = m_pSerial->pack_int16_array( m_pBuf, lpuData, nNum );
            return ret;
        }
		int pack_int16( xos::xos_u16 uData )
		{
			int ret = 0;
			ret = m_pSerial->pack_int16( m_pBuf, uData );
			return ret;
		}

        //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        // 
        int un_pack_int32_array( xos::xos_u32 * lpuData, int nNum, int * lpnNum, int * lpNumNeeded )
        {
            int ret = 0;
            ret = m_pSerial->un_pack_int32_array( m_pBuf, lpuData, nNum, lpnNum, lpNumNeeded );
            return ret;
        }
		int un_pack_int32( xos::xos_u32 * lpuData )
		{
			int ret = 0;
			ret = m_pSerial->un_pack_int32( m_pBuf, lpuData );
			return ret;
		}
        int pack_int32_array( int * lpnAbsolutePos, xos::xos_u32 * lpuData, int nNum )
        {
            int ret = 0;
            ret = m_pSerial->pack_int32_array( m_pBuf, lpnAbsolutePos, lpuData, nNum );
            return ret;
        }
		int pack_int32( int * lpnAbsolutePos, xos::xos_u32 uData )
		{
			int ret = 0;
			ret = m_pSerial->pack_int32( m_pBuf, lpnAbsolutePos, uData );
			return ret;
		}
        int pack_int32_array( xos::xos_u32 * lpuData, int nNum )
        {
            int ret = 0;
            ret = m_pSerial->pack_int32_array( m_pBuf, lpuData, nNum );
            return ret;
        }
		int pack_int32( xos::xos_u32 uData )
		{
			int ret = 0;
			ret = m_pSerial->pack_int32( m_pBuf, uData );
			return ret;
		}

        //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        // 
        int un_pack_int64_array( xos::xos_u64 * lpuData, int nNum, int * lpnNum, int * lpNumNeeded )
        {
            int ret = 0;
            ret = m_pSerial->un_pack_int64_array( m_pBuf, lpuData, nNum, lpnNum, lpNumNeeded );
            return ret;
        }
		int un_pack_int64( xos::xos_u64 * lpuData )
		{
			int ret = 0;
			ret = m_pSerial->un_pack_int64( m_pBuf, lpuData );
			return ret;
		}
        int pack_int64_array( int * lpnAbsolutePos, xos::xos_u64 * lpuData, int nNum )
        {
            int ret = 0;
            ret = m_pSerial->pack_int64_array( m_pBuf, lpnAbsolutePos, lpuData, nNum );
            return ret;
        }
		int pack_int64( int * lpnAbsolutePos, xos::xos_u64 uData )
		{
			int ret = 0;
			ret = m_pSerial->pack_int64( m_pBuf, lpnAbsolutePos, uData );
			return ret;
		}
        int pack_int64_array( xos::xos_u64 * lpuData, int nNum )
        {
            int ret = 0;
            ret = m_pSerial->pack_int64_array( m_pBuf, lpuData, nNum );
            return ret;
        }
		int pack_int64( xos::xos_u64 uData )
		{
			int ret = 0;
			ret = m_pSerial->pack_int64( m_pBuf, uData );
			return ret;
		}

        //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        // 
        int un_pack_float_array( float * lpfData, int nNum, int * lpnNum, int * lpNumNeeded )
        {
            int ret = 0;
            ret = m_pSerial->un_pack_float_array( m_pBuf, lpfData, nNum, lpnNum, lpNumNeeded );
            return ret;
        }
		int un_pack_float( float * lpfData )
		{
			int ret = 0;
			ret = m_pSerial->un_pack_float( m_pBuf, lpfData );
			return ret;
		}
        int pack_float_array( int * lpnAbsolutePos, float * lpfData, int nNum )
        {
            int ret = 0;
            ret = m_pSerial->pack_float_array( m_pBuf, lpnAbsolutePos, lpfData, nNum );
            return ret;
        }
		int pack_float( int * lpnAbsolutePos, float fData )
		{
			int ret = 0;
			ret = m_pSerial->pack_float( m_pBuf, lpnAbsolutePos, fData );
			return ret;
		}
        int pack_float_array( float * lpfData, int nNum )
        {
            int ret = 0;
            ret = m_pSerial->pack_float_array( m_pBuf, lpfData, nNum );
            return ret;
        }
		int pack_float( float fData )
		{
			int ret = 0;
			ret = m_pSerial->pack_float( m_pBuf, fData );
			return ret;
		}

        //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        // 
        int un_pack_double_array( double * lpdData, int nNum, int * lpnNum, int * lpNumNeeded )
        {
            int ret = 0;
            ret = m_pSerial->un_pack_double_array( m_pBuf, lpdData, nNum, lpnNum, lpNumNeeded );
            return ret;
        }
		int un_pack_double( double * lpdData )
		{
			int ret = 0;
			ret = m_pSerial->un_pack_double( m_pBuf, lpdData );
			return ret;
		}
        int pack_double_array( int * lpnAbsolutePos, double * lpdData, int nNum )
        {
            int ret = 0;
            ret = m_pSerial->pack_double_array( m_pBuf, lpnAbsolutePos, lpdData, nNum );
            return ret;
        }
		int pack_double( int * lpnAbsolutePos, double dData )
		{
			int ret = 0;
			ret = m_pSerial->pack_double( m_pBuf, lpnAbsolutePos, dData );
			return ret;
		}
        int pack_double_array( double * lpdData, int nNum )
        {
            int ret = 0;
            ret = m_pSerial->pack_double_array( m_pBuf, lpdData, nNum );
            return ret;
        }
		int pack_double( double dData )
		{
			int ret = 0;
			ret = m_pSerial->pack_double( m_pBuf, dData );
			return ret;
		}

        //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        // 
		// 根据xos_box::i_buf中的当前进度和上次的pos, len进度，检查是否可以合下一包数据。考虑需要预留包头的字节数
		//		nRet是pack执行结果，0 : 成功，非0，失败。pack_end用它判断如果处理xos_box::i_buf中的pos和len
		// 返回值定义：
		//		0 : 成功，可以继续，已经读取xos_box::i_buf中的pos和len
		//      1 : 成功，不能继续，已经读取xos_box::i_buf中的pos和len
		//     -1 : 失败，没有操作
		// 
		int pack_end( int & nBufPos, int & nBufLen, int nRet, int nPackageHeadLen )
		{
			int ret = 0;
			ret = m_pSerial->pack_end( m_pBuf, nBufPos, nBufLen, nRet, nPackageHeadLen );
			return ret;
		}
		// 
		// 只是简单的把xos_box::i_buf中的pos和len保存到nBufPos和nBufLen中
		// 
		int pack_begin( int & nBufPos, int & nBufLen )
		{
			int ret = 0;
			ret = m_pSerial->pack_begin( m_pBuf, nBufPos, nBufLen );
			return ret;
		}
	};

}

#endif // __XOSKIT_XOS_CORE_XOS_STL_SRC_PROTOCOL_BASE_BASE_H__
